# LOADING THE ESSENTIAL LIBRARIES
library(tidyr)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
# THE NORMAL PLOT USING PLOT FUNCTION
## HERE IS THE CSV ON CHINA AND INDIA GDP GROWTH RATE FROM 1961 TO 2022
df<-read.csv("D:/Acer/ww.csv")
# looking for the structure of the dataframe
str(df)
## 'data.frame': 63 obs. of 3 variables:
## $ Year : int 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 ...
## $ China: num -27.27 -5.58 10.3 18.18 16.95 ...
## $ India: num 3.72 2.93 5.99 7.45 -2.64 ...
head(df,5)
## Year China India
## 1 1960 -27.27 3.722743
## 2 1961 -5.58 2.931128
## 3 1962 10.30 5.994353
## 4 1963 18.18 7.452950
## 5 1964 16.95 -2.635770
# The plot
plot(df$Year, df$China, type = "l", col = "red",
main = "China vs India", xlab = "Year", ylab = "gdp growth rate")
lines(df$Year, df$India, type = "l", col = "blue")
legend("bottomright", legend = c("China", "India"), col = c("red", "blue"), lty = 1)

# the graph is simple and clean. The inbuild function in R is very simple plotting of the data
# GGPlOT
#loading the ggplot library
library(ggplot2)
# before plotting the graph , I will make it in bit more presesntable manner
new_df <- pivot_longer(df,cols = c("China","India"),
names_to = "country",values_to = "growth_rate")
head(new_df,5)
## # A tibble: 5 × 3
## Year country growth_rate
## <int> <chr> <dbl>
## 1 1960 China -27.3
## 2 1960 India 3.72
## 3 1961 China -5.58
## 4 1961 India 2.93
## 5 1962 China 10.3
# by using the pivot_longer function i have merged the two colums together a single colum with countrries
# the plot
plot <- ggplot(data = new_df, aes(x = Year, y = growth_rate, colour = country))+
geom_line() +
labs(title = "CHINA VS INDIA 1961", x = "Years", y = "GDP Growth Rate")
plot

# library ggplot make the plot more stand out and also add more functionality the graph
# for instance we can point in out graph
plot_point <- ggplot(data = new_df, aes(x = Year, y = growth_rate, colour = country))+
geom_line() +
geom_point()+
labs(title = "CHINA VS INDIA 1961", x = "Years", y = "GDP Growth Rate")
plot_point

# we can make this more plot more advanced and interactive using plotly library
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
plot_interactive <- ggplot(data = new_df, aes(x = Year, y = growth_rate, colour = country,group=country,text=paste("Year:",Year,"<br>Growth_rate",round(growth_rate,2))))+
geom_line() +
geom_point()+
labs(title = "CHINA VS INDIA 1961", x = "Years", y = "GDP Growth Rate")
plot_interactive

ggplotly(plot_interactive,tooltip = "text")